package com.thalesgroup.nl.trtdelft.dein.mapper.example;

import com.thalesgroup.nl.trtdelft.dein.mapper.AbstractMappedWebService;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author AbdelhaqA
 */
public class Login extends AbstractMappedWebService {

    private String listOfHotels = null;
    private String listOfRestaurants = null;
    private String weatherForecast = null;
    private String loginStatus = null;
    boolean loginStatusFlag = false;


    @Override
    public void start() {
        super.start();

        // queryReceive from UI
        String addressCode = (String) queryReceiveVariableContent.get("addressCode");
        String genreCode = (String) queryReceiveVariableContent.get("genreCode");
        String userId = (String) queryReceiveVariableContent.get("userId");
        String passWord = (String) queryReceiveVariableContent.get("passWord");

        HashMap<String, Serializable> qSendContent = new HashMap<>();
        qSendContent.put("addressCode", addressCode);
        qSendContent.put("genreCode", genreCode);
        qSendContent.put("userId", userId);
        qSendContent.put("passWord", passWord);
        
        List<String> capabilities = getRequiredCababilityForQuerySendVariables();

        // querySend to SNIF, TRIP Plan and Restaurant Plan
        for (String capability : capabilities) {
            Map<String, Serializable> map = getQuerySendServiceParameterElements(capability);
            if (map.containsKey("addressCode") && map.containsKey("genreCode")) {
                querySendVariableContent.put(capability, qSendContent);
                sendMapperInformationInterface.sendQuery(capability);
            }else if (map.containsKey("userId") && map.containsKey("passWord")) {
                querySendVariableContent.put(capability, qSendContent);
                sendMapperInformationInterface.sendQuery(capability);
            }
        }        
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public void input(String senderAddress, String inputVariableName, Serializable input) {
        
        System.out.println("senderAddress=" + senderAddress + "inputVariableName=" + inputVariableName + " input=" + input);
        
        if (inputVariableName.equals("loginStatus") ){
            loginStatus = (String) input; 
            outputs.put("loginStatus", loginStatus);
            loginStatusFlag = Boolean.parseBoolean(loginStatus);
            sendMapperInformationInterface.sendOutput();
        } else if (inputVariableName.equals("listOfHotels") &&  loginStatusFlag){
            listOfHotels = (String) input; 
            HashMap<String, Serializable> qSendContent = new HashMap<>();
            qSendContent.put("listOfHotels", listOfHotels);
            List<String> capabilities = getRequiredCababilityForQuerySendVariables();
           for (String capability : capabilities) {
                if (capability.equals("Aip")) {
                    querySendVariableContent.put(capability, qSendContent);
                    sendMapperInformationInterface.sendQuery(capability);
                }
            }               
            outputs.put("listOfHotels", listOfHotels);
        } else if (inputVariableName.equals("listOfRestaurants") && loginStatusFlag){
            listOfRestaurants = (String) input; 
            HashMap<String, Serializable> qSendContent = new HashMap<>();
            qSendContent.put("listOfRestaurants", listOfRestaurants);
            List<String> capabilities = getRequiredCababilityForQuerySendVariables();
           for (String capability : capabilities) {
                if (capability.equals("Aip")) {
                    querySendVariableContent.put(capability, qSendContent);
                    sendMapperInformationInterface.sendQuery(capability);
                }
            }     
            outputs.put("listOfRestaurants", listOfRestaurants);
        } else if (inputVariableName.equals("weatherForecast")&& loginStatusFlag){
            weatherForecast = (String) input;   
            outputs.put("weatherForecast", weatherForecast);
        }      
        if(outputs.get("loginStatus") != null && outputs.get("listOfHotels") != null && outputs.get("listOfRestaurants") != null && outputs.get("weatherForecast") != null){  
            sendMapperInformationInterface.sendOutput();
        }
            
    }

    @Override
    public void computeOutput() throws IllegalStateException {
    }

    @Override
    public void computeOutput(String variableName) throws IllegalStateException {
        computeOutput();
    }

    @Override
    public Serializable getCurrentOutput(String variableName) throws IllegalStateException {
        return outputs.get(variableName);
    }

}
